package com.admin.thread;

import java.sql.SQLException;
import java.util.Date;
import java.util.concurrent.BlockingQueue;

import org.apache.commons.lang.time.DateUtils;
import org.apache.log4j.Logger;

import com.admin.bean.MobileUserBean;
import com.admin.bean.TempOrderBean;
import com.admin.dao.MobileUserDao;
import com.admin.dao.WasteOrderDao;
import com.admin.service.ConsumeOrderService;

public class DealTempOrderThread implements Runnable {

	private static final Logger log = Logger.getLogger(DealTempOrderThread.class);
	private MobileUserDao mobileUserDao;
	private ConsumeOrderService consumeOrderService;
	private WasteOrderDao wasteOrderDao;

	private BlockingQueue<TempOrderBean> dataQueue;

	public DealTempOrderThread(BlockingQueue<TempOrderBean> dataQueue, MobileUserDao mobileUserDao, ConsumeOrderService consumeOrderService,
			WasteOrderDao wasteOrderDao) {
		this.dataQueue = dataQueue;
		this.mobileUserDao = mobileUserDao;
		this.consumeOrderService = consumeOrderService;
		this.wasteOrderDao = wasteOrderDao;
	}

	@Override
	public void run() {
//		while (!Thread.interrupted()) {
//			if (dataQueue.size() > 0) {// 如果队列不为空
//				try {
//					TempOrderBean tempOrderBean = dataQueue.take();
//					MobileUserBean user = mobileUserDao.findByTelPhone(tempOrderBean.getTelPhone());
//					if (user != null) {
//						log.info("处理订单："+tempOrderBean.getTempId());
//						if(consumeOrderService.dealOrder(tempOrderBean.getPort(), tempOrderBean.getCommand(), tempOrderBean.getConsume(), user)){
//							consumeOrderService.delTempOrder(tempOrderBean.getTempId());
//						}
//					}else{
//						Date date = DateUtils.addHours(tempOrderBean.getCreateTime(), 1);
//						Date now = new Date();
//						if(date.getTime()<=now.getTime()){
//							log.info("超过一个小时未处理，该订单移到废表");
//							consumeOrderService.delTempOrder(tempOrderBean.getTempId());
//							wasteOrderDao.insertWasteOrder(tempOrderBean);
//						}
//					}
//				} catch (InterruptedException e) {
//					e.printStackTrace();
//				} catch (SQLException e) {
//					e.printStackTrace();
//				}
//			} else {// 如果队列为空，线程休眠
//				log.info("队列为空,处理订单线程休眠5分钟===>");
//				try {
//					Thread.sleep(5 * 60 * 1000);
//				} catch (InterruptedException e) {
//					e.printStackTrace();
//				}
//			}
//		}
	}

}
